package com.zl.quickdoc.utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.zl.quickdoc.resolver.JavaParserTagConverterRegistrar;
import com.zl.quickdoc.resolver.converter.AbstractJavaDocTag;
import com.zl.quickdoc.resolver.converter.JavaParserTagConverter;

/**
 * 文档标签工具
 *
 * @author zhouliang
 *
 */
public class JavaTagUtils {

    /**
     * 获取所有标签
     */
    public static List<AbstractJavaDocTag<?>> docTagHandler(List<String> comments) {
        List<AbstractJavaDocTag<?>> docTagList = new ArrayList<>(comments.size());
        for (int i = 0; i < comments.size(); i++) {
            String c = comments.get(i);
            String tagType = CommentUtils.getTagType(c);
            if (StringUtils.isBlank(tagType)) {
                continue;
            }
            JavaParserTagConverter converter = JavaParserTagConverterRegistrar.getInstance().getConverter(tagType);
            AbstractJavaDocTag<?> docTag = converter.converter(c);
            if (docTag != null) {
                docTagList.add(docTag);
            }
        }
        return docTagList;
    }
    
    /**
     * 查找List里面tag名称符合的第一个Tag信息
     *
     * @param list Tag集合
     * @param name AbstractJavaDocTag.name, 如@return
     * @return 符合的第一个Tag信息, 如果没有则返回null
     */
    @SuppressWarnings("unchecked")
    public static <T> AbstractJavaDocTag<T> findTag(List<AbstractJavaDocTag<?>> list, String name) {
        if(list==null || list.isEmpty()) {
            return null;
        }
        for (AbstractJavaDocTag<?> docTag : list) {
            if (docTag.getTagName().equals(name)) {
                return (AbstractJavaDocTag<T>) docTag;
            }
        }
        return null;
    }

    /**
     * 查找List里面tag名称符合的多个Tag信息
     *
     * @param list Tag集合
     * @param name AbstractJavaDocTag.name, 如@param
     * @return 符合的所有Tag信息, 如果没有则返回size=0的List
     */
    @SuppressWarnings("unchecked")
    public static <T> List<AbstractJavaDocTag<T>> findTags(List<AbstractJavaDocTag<?>> list, String name) {
        List<AbstractJavaDocTag<T>> docTags = new ArrayList<>();
        if(list==null || list.isEmpty()) {
            return docTags;
        }
        for (AbstractJavaDocTag<?> docTag : list) {
            if (docTag.getTagName().equals(name)) {
                docTags.add((AbstractJavaDocTag<T>) docTag);
            }
        }
        return docTags;
    }
}
